Game Map Analysis

Preparation


In [ ]:
import matplotlib.image as image
%run "../Utilities/Preparation.ipynb"

Functions


In [ ]:
# eg _types = ['death', 'reach', 'add', 'craft', 'remove', 'select', 'selectmenu']

def filterAndLocateEvents(_types, _rmDF = rmdf152):
    _mapRelevantColumns = ['type', 'section', 'coordinates']
    _locatedEventsDF = _rmDF.loc[:,_mapRelevantColumns].dropna()

    # types
    _locatedEventsDF = _locatedEventsDF[_locatedEventsDF['type'].isin(_types)]

    # section
    _locatedEventsDF = _locatedEventsDF[(_locatedEventsDF['section'].str.startswith('tutorial1'))]

    # x,y
    _locatedEventsDF['x'] = _locatedEventsDF['coordinates']
    _locatedEventsDF['y'] = _locatedEventsDF['coordinates']

    _f = FloatProgress(min=0, max=len(_locatedEventsDF.index))
    display(_f)

    for index in _locatedEventsDF.index:
        _coordinates = re.findall('-*\d+', _locatedEventsDF['coordinates'][index])
        _locatedEventsDF['x'][index] = int(_coordinates[0])
        _locatedEventsDF['y'][index] = int(_coordinates[1])
        _f.value += 1

    _locatedEventsDF = _locatedEventsDF.drop('coordinates', 1)

    return _locatedEventsDF

In [ ]:
def getConstants():
    _gameStartCoordinates = [-229,-608]
    
    # from RedMetrics
    #_gameEndCoordinates = [441,-472]
    
    # after tinkering
    _gameEndCoordinates = [450,-475]
    

    # only for '../../images/map.tutorial1.png'
    _imgStartCoordinates = [833,432]
    _imgEndCoordinates = [1333,334]

    _scaleX = (_gameStartCoordinates[0]-_gameEndCoordinates[0])/(_imgStartCoordinates[0]-_imgEndCoordinates[0])
    _scaleY = (_gameStartCoordinates[1]-_gameEndCoordinates[1])/(_imgStartCoordinates[1]-_imgEndCoordinates[1])
    
    _offsetX = _gameStartCoordinates[0] - (_imgStartCoordinates[0]*_scaleX)
    _offsetY = _gameStartCoordinates[1] - (_imgStartCoordinates[1]*_scaleY)
    
    _constants = pd.Series([_scaleX,_scaleY,_offsetX,_offsetY], index=['scaleX', 'scaleY', 'offsetX', 'offsetY'])
    
    return _constants
    
def getGraphPosition(_imgPos, _constants):
    _x = getXGraphPosition(_imgPos[0], _constants)
    _y = getYGraphPosition(_imgPos[1], _constants)
    return [_x, _y]

def getXGraphPosition(_xImg, _constants):
    return _constants.scaleX*_xImg + _constants.offsetX

def getYGraphPosition(_yImg, _constants):
    return _constants.scaleY*_yImg + _constants.offsetY

In [ ]:
def plotLocatedEvents(_XYEventsDF, _types):
    
    _constants = getConstants()

    plt.figure(figsize=(18,12))

    # background map image display
    _mapBackground = image.imread('../../images/map.tutorial1.png')

    # background image dimensions
    _minX = 0
    _maxX = _mapBackground.shape[1]
    _minY = _mapBackground.shape[0]
    _maxY = 0

    plt.imshow(_mapBackground, aspect='auto', zorder=-1, \
               extent=(getXGraphPosition(_minX,_constants), \
                       getXGraphPosition(_maxX,_constants), \
                       getYGraphPosition(_minY,_constants), \
                       getYGraphPosition(_maxY,_constants)), \
               alpha=0.5)


    _colors = ['blue', 'red', 'green', 'yellow', 'black', 'cyan', 'orange', 'purple', 'brown', 'pink', 'olive', 'cyan', ]
    _alphas = [ 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, ]
    _scatters = {}

    for i, t in enumerate(_types):
      _pts = _XYEventsDF[_XYEventsDF['type'] == t]
      x = _pts['x']
      y = _pts['y']

      _scatters[t] = plt.plot(x, y, 'o', c=_colors[i], lw=0, alpha=_alphas[i])

    plt.legend([ x[0] for x in _scatters.values()], list(_scatters.keys()))

    # graph dimensions and extrema on tutorial1
    # _minX=-1152
    # _maxX=539
    # _minY=-1161
    # _maxY=-100
    plt.xlim([-1200, 600])
    plt.ylim([-1220, -20])
    
    plt.show()